// 进入登陆
function login_do() {
    //输入验证
    if($("#bot_name").val() == ""){
        window.alert("用户名不能为空！");
        $("#bot_name").focus();
        return false;
    }
    if($("#bot_password").val() == "") {
        window.alert("密码不能为空！");
        $("#bot_password").focus();
        return false;
    }
    //js验证 `~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？  
    var pattern = new RegExp("[~'!@#$%^&*()-+_=:]");  
    if($("#bot_name").val() != "" && $("#bot_name").val() != null) {
        if(pattern.test($("#bot_name").val())) {
            window.alert("用户名格式不正确！");
            $("#bot_name").focus();
            return false;
        }
    }  
    if($("#bot_password").val() != "" && $("#bot_password").val() != null) {  
        if(pattern.test($("#bot_password").val())) {
            window.alert("密码格式不正确！");
            $("#bot_password").focus();
            return false;  
        }  
    }


    var bot_name = $('#bot_name').val();
    var bot_password = $('#bot_password').val();
    // Ajax调用处理
    var login_do_return = $.ajax({
        type: "POST",
        url: "index.php?m=cloud&c=cloud_login&a=login_oper",
        data: "bot_name="+bot_name+"&bot_password="+bot_password,
        async: false
    }).responseText;

    if (login_do_return == "1"){
        window.alert("用户名不存在，请核对后重新登陆！");
    } else if (login_do_return == "2") {
        window.alert("密码不正确，请核对后重新登陆！");
    } else if (login_do_return == "3") {
        window.alert("机器人已过期，请通知管理员进行操作！");
    } else if (login_do_return == "4") {
        location.href="index.php?m=cloud&c=cloud_my&a=my_main";
    } else {
        window.alert("请勿提交非法数据！");
    }
}

// 离开主界面
function login_exit() {
    // Ajax调用处理
    var login_do_return = $.ajax({
        type: "POST",
        url: "index.php?m=cloud&c=cloud_login&a=login_exit",
        data: "",
        async: false
    }).responseText;
    if (login_do_return == "1"){
        location.href="index.php?m=cloud&c=cloud_login&a=login_main";
    }
}

// 获取发送群处理
function send_group_data(serial) {
    var login_do_return = $.ajax({
        type: "POST",
        url: "index.php?m=cloud&c=cloud_my&a=get_send_group",
        data: "serial="+serial,
        async: false
    }).responseText;
    if (login_do_return == "1") {
        window.alert("该群可能已经被删除，请通知管理员！");
    } else if (login_do_return == "2") {
        location.href="index.php?m=cloud&c=cloud_group&a=my_group";
    } else {
        window.alert("请勿提交非法数据！");
    }
}

// 离开群界面
function my_exit() {
    // Ajax调用处理
    var login_do_return = $.ajax({
        type: "POST",
        url: "index.php?m=cloud&c=cloud_my&a=exit_send_group",
        data: "",
        async: false
    }).responseText;
    if (login_do_return == "1"){
        location.href="index.php?m=cloud&c=cloud_my&a=my_main";
    }
}

// 设置群数据
function set_group_data() {
    function compare_time(time1, time2) {
        for (var i = 0; i <= 4; ++i) {
            if (time1[i] != time2[i]) {
                return time1[i] > time2[i] ? true : false;
            }
        }
        return false
    }

    // 输入验证
    if($("#g_bot_begin_work").val() == "") {
        window.alert("发单开始时间不能为空！");
        $("#g_bot_begin_work").focus();
        return false;
    }

    if($("#g_bot_end_work").val() == "") {
        window.alert("发单结束时间不能为空！");
        $("#g_bot_end_work").focus();
        return false;
    }

    if($("#g_bot_statue").val() == "") {
        window.alert("发单状态不能为空！");
        $("#g_bot_statue").focus();
        return false;
    }

    if($("#g_interval").val() == "") {
        window.alert("发单间隔不能为空！");
        $("#g_interval").focus();
        return false;
    }

    if($("#g_lunch_break").val() == "") {
        window.alert("发单间隔不能为空！");
        $("#g_lunch_break").focus();
        return false;
    }

    // 判断时间格式是否符合
    var pattern = new RegExp("^([01][0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?");
    if(!(pattern.test($("#g_bot_begin_work").val()) || $("#g_bot_begin_work").val() == "24:00") || $("#g_bot_begin_work").val().length != 5 ) {
        $("#g_bot_begin_work").focus();
        window.alert("发单开始时间格式不对！");
        return false
    }

    if(!(pattern.test($("#g_bot_end_work").val()) || $("#g_bot_end_work").val() == "24:00") || $("#g_bot_end_work").val().length != 5 ) {
        $("#g_bot_end_work").focus();
        window.alert("发单结束时间格式不对！");
        return false
    }

    pattern = new RegExp("[0-1]");
    if(!pattern.test($("#g_bot_statue").val()) || $("#g_bot_statue").val().length != 1 ) {
        $("#g_bot_statue").focus();
        window.alert("发单状态格式不对！");
        return false
    }


    if (!compare_time($("#g_bot_end_work").val(), $("#g_bot_begin_work").val())) {
        $("#g_bot_begin_work").focus();
        $("#g_bot_end_work").focus();
        window.alert("发单结束时间比开始时间小！");
        return false
    }

    var g_bot_begin_work = $('#g_bot_begin_work').val()
    var g_bot_end_work = $('#g_bot_end_work').val()
    var g_bot_statue = $('#g_bot_statue').val()
    var g_interval = $('#g_interval').val()
    var g_lunch_break = $('#g_lunch_break').val()
    var g_taobao_ance = $('#g_taobao_ance').val()
    var g_pdd_ance = $('#g_pdd_ance').val()
    var g_jd_ance = $('#g_jd_ance').val()
    var g_with_preferred = $('#g_with_preferred').val()
    var g_good_type = ""
    var el = document.getElementsByTagName('input');
	var len = el.length;
	for(var i = 0; i < len; ++i) {
		if(el[i].type == "checkbox") {
            if (el[i].checked) {
                if (g_good_type.length != 0) {
                    g_good_type += "," + el[i].value
                } else {
                    g_good_type += el[i].value
                }
            }
		}
    }
    
    // Ajax调用处理
    var login_do_return = $.ajax({
        type: "POST",
        url: "index.php?m=cloud&c=cloud_group&a=set_group_data",
        data: "g_with_preferred="+g_with_preferred+"&g_bot_begin_work="+g_bot_begin_work+"&g_bot_end_work="+g_bot_end_work+"&g_taobao_ance="+g_taobao_ance+"&g_pdd_ance="+g_pdd_ance+"&g_jd_ance="+g_jd_ance+"&g_bot_statue="+g_bot_statue+"&g_interval="+g_interval+"&g_lunch_break="+g_lunch_break+"&g_good_type="+g_good_type,
        async: false
    }).responseText;

    if (login_do_return == "1"){
        window.alert("请求超时，请重新请求！");
        location.href="index.php?m=cloud&c=cloud_my&a=my_main";
    } else if (login_do_return == "2") {
        location.href="index.php?m=cloud&c=cloud_group&a=my_group";
    } else {
        window.alert("请勿提交非法数据！");
    }
}

// 设置点击事件
$(document).on("click", ".li-input6", function () {
    if (this.name == '全部') {
        if (this.checked) {
            var el = document.getElementsByTagName('input');
            var len = el.length;
            for(var i = 0; i < len; ++i) { 
            	if(el[i].type == 'checkbox') {
                    if (el[i].name != '全部') {
                        el[i].checked = false
                    }
            	}
            }
        }
    } else {
        if (this.checked) {
            var el = document.getElementsByTagName('input');
            var len = el.length;
            for(var i = 0; i < len; ++i) { 
            	if(el[i].type == "checkbox") {
                    if (el[i].name == '全部') {
                        el[i].checked = false
                    }
            	}
            }
        }
    }

    // 判断是否全部选取
    var is_all_check = true
    var el = document.getElementsByTagName('input');
    var len = el.length;
    for(var i = 0; i < len; ++i) { 
        if(el[i].type == "checkbox") {
            if (el[i].name != '全部') {
                if (!el[i].checked) {
                    is_all_check = false
                    break
                }
            }
        }
    }

    if (is_all_check) {
        for(var i = 0; i < len; ++i) { 
            if(el[i].type == "checkbox") {
                if (el[i].name == '全部') {
                    el[i].checked = true
                } else {
                    el[i].checked = false
                }
            }
        }
    }
});

